package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.IotDeviceWaterQualityLogDao;
import cn.topevery.garden.entity.po.IotDeviceWaterQualityLog;
import cn.topevery.garden.entity.query.IotDeviceWaterQualityLogQuery;
import cn.topevery.garden.entity.vo.IotDeviceWaterQualityLogVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 
 *
 * @author code-generate
 * @date 2021-09-26
 */
@Repository
public class IotDeviceWaterQualityLogDaoImpl extends BaseDaoImpl<IotDeviceWaterQualityLog> implements IotDeviceWaterQualityLogDao {
    @Override
    public <P extends DbCsp> List<IotDeviceWaterQualityLog> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<IotDeviceWaterQualityLogVO> listPage(IotDeviceWaterQualityLogQuery query) {
        String table = "t_iot_device_water_quality_log tidwql";

        String columns = "tidwql.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tidwql.c_id desc");
        sqlQueryMate
                .where(true, "tidwql.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "tidwql.c_device_code", ScriptConditionEnum.Eq, query.getCode())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, IotDeviceWaterQualityLogVO.class);
    }

    @Override
    public IotDeviceWaterQualityLog getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public IotDeviceWaterQualityLogVO getLastDayRecordByCode(String code) {
        String sql = "select t.* from t_iot_device_water_quality_log t where t.c_db_status = 0 and t.c_device_code = '"+ code +"' and t.c_db_create_date >= DATE_SUB(now(),INTERVAL 1 DAY) order by t.c_db_create_date asc limit 1";
        AtomicReference<IotDeviceWaterQualityLogVO> vo = new AtomicReference<>();
        run(sql, CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToEntity(IotDeviceWaterQualityLogVO.class));
        });
        return vo.get();
    }
}
